// pages/orders/orders.js
const app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.id){
      this.setData({
        currentTab: options.id
      })
      getUserOrders(that, options.id)
    }

    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          imageUrl: app.globalData.imageUrl
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getPhone(this)
  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      getUserOrders(that, e.target.dataset.current);
    }
  },
  calling:function(e){
   // console.log(e)
   var that=this;
    wx.makePhoneCall({
      phoneNumber: '' + that.data.shop_phones,
    })
  }
  ,
  cancelOrder:function(e){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否取消订单',
      success:function(res){
        if(res.confirm){
          cancelOrder(that,e.target.id,e.target.dataset.money)
        }
      }
    })
  }
  ,
  deleteOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除订单',
      success: function (res) {
        if (res.confirm) {
          deleteOrder(that, e.target.id)
        }
      }
    })
  }
  ,
  sureReceve:function(e){
    var that=this;
    wx.request({
      url: app.globalData.allUrl + "/orderr/collect",
      data: {
      //  user_id:wx.getStorageSync("userid"),//如果不是分销版，就不用传
      //  goods_id: e.target.dataset.goodsid,//如果不是分销版，就不用传
        id: e.target.id,
       // money: e.target.dataset.money//如果不是分销版，就不用传
      },
      header: { 'content-type': "application/json" },
      method: 'POST',
      success: function (res) {
       if(res.data==200){
         getUserOrders(that,1);
       }else if(res.data==400){
         wx.showModal({
           title: '提示',
           content: '确认收货失败，请联系商家！',
         })
       }
      }
    })
  }
})

function getUserOrders(that,_id){
  var flag='';
  if(_id==0){
    flag = 'delivery'
  } else if (_id == 1) {
    flag = 'shipped'
  } else if (_id == 2) {
    flag = 'completed'
  }
  wx.request({
    url: app.globalData.allUrl + "/orderr/class_order",
    data: {
      class: flag,
      user_id:wx.getStorageSync('userid')
    },
    header: { 'content-type': "application/json" },
    method: 'POST',
    success: function (res) {
      console.log(res.data)
      that.setData({
        orders:res.data
      })
    }
  })
}


function cancelOrder(that, order_id, money){
  wx.request({
    url: app.globalData.allUrl + "/wxpay/wx_return",
    data: {
      order_id: order_id,
      user_id: wx.getStorageSync('userid'),
      money: money
    },
    header: { 'content-type': "application/json" },
    method: 'POST',
    success: function (res) {
      wx.showModal({
        title: '提示',
        content: '退款将会在1-7个工作日返回给您，请注意查收',
      })
    }
  })
}

function deleteOrder(that, id) {
  wx.request({
    url: app.globalData.allUrl + "/orderr/delete",
    data: {
     id:id
    },
    header: { 'content-type': "application/json" },
    method: 'POST',
    success: function (res) {
      console.log(res.data);
      if(res.data==200){
        getUserOrders(that, 2);
      } else if (res.data == 400){
        
      }
    }
  })
}

function getPhone(that) {
  wx.request({
    url: app.globalData.allUrl + "/user/get_phone",
    data: {
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      that.setData({
        shop_phones: res.data[0].phone
      })
    }
  })
}